package com.dz.scrm.mvp.ui.fragment

import android.graphics.Typeface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.dz.scrm.R
import com.dz.scrm.mvp.model.entity.CompanyInfoBean
import com.dz.scrm.mvp.model.entity.LoginBean
import com.dz.scrm.mvp.presenter.DZPresent
import com.dz.scrm.mvp.ui.ChooseCompany
import com.dz.scrm.mvp.ui.LoginActivity
import com.dz.scrm.mvp.ui.mine.AboutActivity
import com.dz.scrm.utils.*
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.tbruyelle.rxpermissions2.RxPermissions
import com.zackratos.ultimatebarx.ultimatebarx.addStatusBarTopPadding
import kotlinx.android.synthetic.main.fragment_mine.*
import me.jessyan.art.base.BaseFragment
import me.jessyan.art.integration.AppManager
import me.jessyan.art.utils.ArtUtils

/**
 * 作者：fanda
 * 日期：2021/4/23 11:51
 * 描述：我的
 */
class MineFragment : BaseFragment<DZPresent>() {

    private var mSwitchAccountDialog: BasePopupView? = null

    private val companyInfoBean by lazy {
        getInfoBean<CompanyInfoBean>(SharedPreferencesUtil.COMPANY_INFO)
    }

    companion object {
        @JvmStatic
        fun getInstance(): MineFragment {
            return MineFragment().apply {
            }
        }
    }

    override fun initView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.fragment_mine, container, false)
    }

    override fun initData(savedInstanceState: Bundle?) {
        initListeners()
        iv_user_avatar.addStatusBarTopPadding()
        tv_phone_num.typeface = Typeface.defaultFromStyle(Typeface.BOLD)//加粗
        if (companyInfoBean != null) {
//            if (companyInfoBean!!.is_hidden!!) {
//                tv_phone_num.text = getEncryptPhoneNum(companyInfoBean!!.account ?: "")
//            } else {
//                tv_phone_num.text = companyInfoBean!!.account ?: ""
//            }
            tv_phone_num.text = companyInfoBean!!.account ?: ""
        }
    }

    private fun initListeners() {
        layout_about.setOnClickListener { ArtUtils.startActivity(AboutActivity::class.java) }
        layout_company.setOnClickListener {
            //获取企业信息失败
            val info = SharedPreferencesUtil.getInfo(SharedPreferencesUtil.LOGIN_INFO)
            if (!StringUtils.isEmpty(info)) {
                val loginBean = KingSoftParasJson.getObjectByJson(info, LoginBean::class.java) as? LoginBean
                if (loginBean != null) {
                    if (loginBean.list?.size!! > 1) {
                        launchActivity<ChooseCompany>(mContext)
                    } else {
                        ArtUtils.snackbarText(getString(R.string.no_more_company))
                    }
                }
            }
        }

        layout_account.setOnClickListener {
            if (mSwitchAccountDialog == null) {
                mSwitchAccountDialog = XPopup.Builder(activity).asConfirm("提示", "切换帐号将退出当前登录") {
                    activity?.switchAccount()
                }
            }
            mSwitchAccountDialog!!.show()
        }
    }

    override fun obtainPresenter() = DZPresent(ArtUtils.obtainAppComponentFromContext(mContext), RxPermissions(this))

    override fun setData(data: Any?) {
    }
}